package com.spbl.service.impl;

import com.spbl.dao.SysUserDao;
import com.spbl.dao.SysUserRoleDao;
import com.spbl.entity.SysUser;
import com.spbl.entity.SysUserRole;
import com.spbl.service.SysUserService;
import com.spbl.vo.SysUserMenuPermissonVo;
import com.spbl.vo.SysUserRoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 
 * @ClassName SysUserServiceImpl
 * @Description 用户serv实现
 * @author:Gaoxueyong  http://blog.csdn.net/fenfenguai
 * @Date 2016年12月8日 下午3:33:28
 * @version 1.0.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {

	@Resource
	private SysUserDao sysUserDao;
	@Resource
	private SysUserRoleDao sysUserRoleDao;
	

	@Override
	public int updateSysUser(SysUser sysUser) {
		//先根据用户id删除用户角色 
		sysUserRoleDao.deleteSysUserRoleByUserId(sysUser.getId());
		
		//插入用户角色
		if(sysUser.getRoleStr()!=null && !"".equals(sysUser.getRoleStr())){
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setUserId(sysUser.getId());
			String[] roleStr = sysUser.getRoleStr().split(",");
			for(String roleId:roleStr){
				if(!"".equals(roleId)){
					sysUserRole.setRoleId(roleId);
					sysUserRoleDao.insertSysUserRole(sysUserRole);
				}
				
			}
		}
		return sysUserDao.updateSysUser(sysUser);
	}

	/**
	 * 
	 * @Description 获取用户列表
	 * @param paramerMap
	 * @return
	 * @author: Gaoxueyong  http://blog.csdn.net/fenfenguai Create at: 2016年12月8日 下午3:06:51
	 */
	@Override
	public List<SysUser> getSysUserList(Map<String, Object> paramerMap) {
		return sysUserDao.getSysUserList(paramerMap);
	}

	
	/**
	 * 
	 * @Description 根据登录名获取用户对象
	 * @param loginName
	 * @return
	 * @author: Gaoxueyong  http://blog.csdn.net/fenfenguai Create at: 2016年12月8日 下午3:06:32
	 */
	@Override
	public SysUser getSysUserByLoginName(String loginName) {
		return sysUserDao.getSysUserByLoginName(loginName);
	}

	@Override
	public List<SysUserRoleVo> getUserRoleInfoByUserId(String userId) {
		return sysUserDao.getUserRoleInfoByUserId(userId);
	}

	@Override
	public List<SysUserMenuPermissonVo> getUserMenuPermissionInfoByUserId(String userId) {
		return sysUserDao.getUserMenuPermissionInfoByUserId(userId);
	}
}
